package com.whnk.rtms.service.impl;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.whnk.rtms.mapper.PrjInfMapper;
import com.whnk.rtms.domain.PrjInf;
import com.whnk.rtms.service.IPrjInfService;
import com.whnk.system.mapper.SequenceMapper;
import com.whnk.common.support.Convert;
import com.whnk.common.utils.DateUtils;

/**
 * 项目 服务层实现
 * 
 * @author whwhnk
 * @date 2019-04-17
 */
@Service("prjinf")
public class PrjInfServiceImpl implements IPrjInfService {
	@Autowired
	private PrjInfMapper prjInfMapper;

	@Autowired
	private SequenceMapper sequenceMapper;

	/**
	 * 查询项目信息
	 * 
	 * @param prjId
	 *            项目ID
	 * @return 项目信息
	 */
	@Override
	public PrjInf selectPrjInfById(String prjId) {
		return prjInfMapper.selectPrjInfById(prjId);
	}

	/**
	 * 查询项目列表
	 * 
	 * @param prjInf
	 *            项目信息
	 * @return 项目集合
	 */
	@Override
	public List<PrjInf> selectPrjInfList(PrjInf prjInf) {
		return prjInfMapper.selectPrjInfList(prjInf);
	}
	
	/**
	 * 查询所有项目列表
	 * 
	 * @return
	 */
	@Override
	public List<PrjInf> selectPrjInfAll() {
		return prjInfMapper.selectPrjInfAll();
	}

	/**
	 * 新增项目
	 * 
	 * @param prjInf
	 *            项目信息
	 * @return 结果
	 */
	@Override
	public int insertPrjInf(PrjInf prjInf) {
		// 获取序列值
		String seqVal = sequenceMapper.getSeqNextVal("seq_prj_inf");
		// 项目编号规则：4位年份+4位序号
		String prjId = DateUtils.dateTimeNow().substring(0, 4) + new DecimalFormat("0000").format(Long.valueOf(seqVal));
		prjInf.setPrjId(prjId);
		prjInf.setLastUdtDtTm(new Date());
		return prjInfMapper.insertPrjInf(prjInf);
	}

	/**
	 * 修改项目
	 * 
	 * @param prjInf
	 *            项目信息
	 * @return 结果
	 */
	@Override
	public int updatePrjInf(PrjInf prjInf) {
		String prjId = prjInf.getPrjId();
		if (StringUtils.isNotBlank(prjId)) {
			return prjInfMapper.updatePrjInf(prjInf);
		} else {
			return 0;
		}
	}

	/**
	 * 删除项目对象
	 * 
	 * @param ids
	 *            需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deletePrjInfByIds(String ids) {
		return prjInfMapper.deletePrjInfByIds(Convert.toStrArray(ids));
	}

}
